// @(#)common.hh
// Time-stamp: <2008-12-22 17:09:36 root>
// Copyright 2008 Julian Qian
// Author: root@localhost.localdomain
// Version: $Id: common.hh,v 0.0 2008/09/27 13:43:19 root Exp $
// Keywords: 
//

#ifndef _COMMON_H
#define _COMMON_H 1

#define DEBUG

// extern "C" {
// #include <stdio.h>
// #include <stdlib.h>
// #include <string.h>
// #include <errno.h>
// #include <utmpx.h>
// #include <sys/msg.h>
// #include <sys/shm.h>
// #include <sys/sem.h>
// #include <sys/types.h>
// #include <sys/wait.h>
// #include <sys/mman.h>
// #include <sys/stat.h>
// #include <unistd.h>
// #include <fcntl.h>


// #include <syslog.h>
// };

// #include <mysql.h>

// #include <iostream>
// #include <iomanip>

// #include "thread.hh"
// #include <boost/thread/thread.hpp>
// #include "mutex.hh"
// #include "db.hh"
// #include "inifile.hh"

// using namespace std;

#ifndef NULL
#define NULL 0
#endif

#ifndef FALSE
#define FALSE 0
#endif

#ifndef TRUE
#define TRUE 1
#endif

#define MAXBUF 4096

#ifndef TRACE_FILE
#define TRACE_FILE "./trace"
#endif

/* Following shortens all the type casts of pointer arguments */
#define	FILE_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
					/* default file access permissions for new files */
#define	DIR_MODE	(FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)
					/* default permissions for new directories */

/* Miscellaneous constants */
#define	MAXLINE		4096	/* max text line length */
#define	MAXSOCKADDR  128	/* max socket address structure size */
#define	BUFFSIZE	8192	/* buffer size for reads and writes */
#define PHONE_SIZE 14   /* mobile phone number length */
#define IP_ANON_SIZE 16 /* ip address length */

#define MAXLEN 50

#define SHM_ESMS_SIZE 5000 /* ws_alarm_receive */
#define SHM_DEVICE "/dev/shm/ehome"

#define GEN_CONF_FILE "/etc/ehome/ehomed.conf"
#define LOG_CONF_FILE "/etc/ehome/log.properties"

#define SERVER_PORT 6800
#define CLIENT_PORT 6800


#define DEVID_NUM_ALL 12  /* device identity numer length */
#define DEVID_NUM 3  /* effective UNIQUE ID bytes length */
#define SEQU_NUM 2 /* sequence bytes length */

#define PROTO_DEVI 3 /* id begin, byte 3rd ~ 5th, 3 bytes */
#define PROTO_SPEC 11 /* device type */
#define PROTO_SEQU 12 /* sequence number, 2 bytes */
#define PROTO_TYPE 15 /* action type */
#define PROTO_DATA 16 /* action data */
#define PROTO_LENG 17
#define PROTO_LENG_TMP 52

/* alarm defense define */
/* ################ 11th byte ################ */
#define SPEC1 0x6C
#define SPEC2 0xD0

#define SPEC_1 1
#define SPEC_2 2
#define SPEC_0 0

/* ################ 15th byte ################ */
#define CLI_RESET 0x0
#define CLI_ALARM 0x2
#define CLI_TIMER 0x4
#define CLI_DEFEN 0x5
#define CLI_DEFEN_AUTO 0xe

#define SER_RESET 0x1
#define SER_ALARM 0x3
#define SER_DEFEN 0x6
#define SER_DEFEN_AUTO 0xf

#define SER_UPDATE 0x7
#define CLI_UPDATE 0x8

/* ################ 16th byte ################ */
/* 15th: 0x02 */
#define ALARM_MANUA 0x01
#define ALARM_DOORR 0x02
#define ALARM_INFRA 0x04
#define ALARM_HELPP 0x03
#define ALARM_SMOKE 0x05
#define ALARM_GASSS 0x06
#define ALARM_GLASS 0x07
#define ALARM_WATER 0x08

#define ALARM_DEFENSE 0x09 // mobile phone type, search in database

#define ALARM_DEFEN_RES 0x10
#define ALARM_UNDEF_RES 0x11

#define MSG_DEFEN_RES "defense ok"
#define MSG_UNDEF_RES "undefense ok"
#define MSG_ALARM "default alarm"

/* 15th: 0x05 */
/* 15th: 0x07 */
#define CTRL_DEFEN 0x30
#define CTRL_UNDEF 0x00
#define CTRL_NULL 0x00

/* phone msg from user */
#define DEFEN_MSG "bf"
#define UNDEF_MSG "cf"


/* ################ these status ################ */
#define STAT_NULLL 0 /* undefined */
#define STAT_RESET 1 /* device init .. */
#define STAT_TIMER 2 /* normal poll query, every 4 min */

#define STAT_DEFEN 3 /* defense status, according to timer and response */
#define STAT_UNDEF 4 /* un-defense status, according to timer and response */
#define STAT_BUTON 23 /* defense by tele controller button*/
#define STAT_UNBTN 24 /* un-defense by tel controller button */
#define STAT_BYWEB 8 /* defense from website */
#define STAT_UNWEB 9 /* un-defense from website */
#define STAT_PHONE 10 /* defense from phone */
#define STAT_UNPHO 11 /* un-defense from phone */

#define STAT_ALARM 5 /* alarm by tel controller */
#define STAT_DOORR 6 /* door alarm */
#define STAT_INFRA 7 /* infrared alarm */

#define STAT_OFFLI 12 /* offline */
#define STAT_OTHER 13 /* other... */
#define STAT_HELPP 14 /* emergency help */
#define STAT_SMOKE 15 /* smoke alarm */
#define STAT_GASSS 16 /* gas alarm */
#define STAT_GLASS 17 /* glass broken alarm */
#define STAT_WATER 18 /* water alarm */
/* struct define */

#ifndef __cplusplus
typedef short bool;
#endif
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
typedef unsigned long devid_t;
typedef int stat_t;
typedef int spec_t;
typedef unsigned long ip_t;
typedef unsigned int uid_t;
typedef int locked_t;
typedef char type_t;
typedef char alarm_t;  // only one byte for sendto()


#endif /* _COMMON_H */

